#include "threadPool.h"
int makeWorker(threadPool_t *threadPool){
    for(int i = 0; i < threadPool->workerNum; ++i){
        pthread_create(&threadPool->tidArr[i],NULL,threadFunc,threadPool);
    }
    return 0;
}


void *threadFunc(void *arg){
    threadPool_t *threadPool = (threadPool_t *)arg;
    char cmd[10]={0};
    char client_arg[100]={0};
    char buf[100]={0};
    char name[30]={0};
    char password[100]={0};
    pathStack_t * client_stack =(pathStack_t *)calloc(1,sizeof(pathStack_t));
    InitStack(client_stack);
    while(1){
        pthread_mutex_lock(&threadPool->taskQueue.mutex);
        while(threadPool->taskQueue.queueSize == 0&&threadPool->exitflag==0){
            pthread_cond_wait(&threadPool->taskQueue.cond, &threadPool->taskQueue.mutex);
        }
        if(threadPool->exitflag != 0){
            printf("child is going to exit!\n");
            pthread_mutex_unlock(&threadPool->taskQueue.mutex);
            pthread_exit(NULL);
        }
        printf("child get a task!\n");
        int netfd = threadPool->taskQueue.pFront->netfd;
        deQueue(&threadPool->taskQueue);
        pthread_mutex_unlock(&threadPool->taskQueue.mutex);

        while(1)
        {
            memset(buf,0,sizeof(buf));
            memset(name,0,sizeof(name));
            memset(password,0,sizeof(password));
            recv_msg(netfd,name);
            recv_msg(netfd,password);
            int pass_ret=passwd_check(name,password,buf);
            printf("%s\n",buf);
            send_msg(netfd,buf);
            if(pass_ret==0)
            {
                NETDISK_LOG_INFO(name,"\0","已连接");
                memset(buf,0,sizeof(buf));
                cd(name,client_stack,buf);
                send_msg(netfd,"已进入家目录，目前文件有：\n");
                char a[10]={0};
                ls(a,buf,client_stack);
                send_msg(netfd,buf);
                
                break;
            }
        }

        while(1)
        {
            char server_exit[]="yes";

            send(netfd,server_exit,sizeof(server_exit),0);
            memset(cmd,0,sizeof(cmd));
            memset(client_arg,0,sizeof(client_arg));
            bzero(buf,sizeof(buf));
            
            recv_msg(netfd,cmd);
            recv_msg(netfd,client_arg);
            
            NETDISK_LOG_INFO(name,cmd,client_arg);
            if(cmd[0]=='\0')
            {
                break;
            }
            if(strcmp(cmd,"cd")==0)
            {
                int cd_ret=cd(client_arg,client_stack,buf);
                if(cd_ret==-1)
                {
                    sprintf(buf,"cd the wrong directory");
                }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"mkdir")==0)
            {
                int mkdir_ret=mkdir_server(client_arg,client_stack);
                if(mkdir_ret==-1)
                {
                    sprintf(buf,"make the wrong directory");
                }
                else
                {
                    ls("\0",buf,client_stack);
                }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"ls")==0)
            {
               int ls_ret= ls(client_arg,buf,client_stack);
               if(ls_ret==-1)
               {
                   sprintf(buf,"ls the wrong directory");
               }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"puts")==0)
            {
                char path_puts[100]={0};
                pwd(client_stack,path_puts);
                strcat(path_puts,client_arg);
               getsfile(netfd,path_puts); 
            }
            else if(strcmp(cmd,"gets")==0)
            {
                recv_msg(netfd,client_arg);
                char path_gets[100]={0};
                pwd(client_stack,path_gets);
                strcat(path_gets,client_arg);
                int ret=open(path_gets,O_RDWR,0666);
                if(ret==-1){
                    send(netfd,&ret,sizeof(ret),0);
                    continue;
                }
                send(netfd,&ret,sizeof(ret),0);
                transfile(netfd,path_gets);
                bzero(buf,sizeof(buf));
                recv(netfd,buf,sizeof(buf),0);
                if(buf[0]=='\0'){
                    break;
                }
            }
            else if(strcmp(cmd,"pwd")==0)
            {
                cd(client_arg,client_stack,buf);
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"remove")==0)
            {
                char path_arg[100]={0};
                pwd(client_stack,path_arg);
                strcat(path_arg,client_arg);
                printf("删除路径是:%s\n",path_arg);
                if(unlink(path_arg))
                {
                    sprintf(buf,"删除失败\n");
                }
                else
                {
                    sprintf(buf,"删除成功\n");
                }
                send_msg(netfd,buf);
            }
            else if(strcmp(cmd,"rmdir")==0)
            {
                char path_arg[100]={0};
                pwd(client_stack,path_arg);
                strcat(path_arg,client_arg);
                if(rmdir(path_arg))
                {
                    sprintf(buf,"删除失败\n");
                }
                else
                {
                    sprintf(buf,"删除成功");
                }
                send_msg(netfd,buf);
            }

            printf("child finish a task!\n");
        }
        printf("用户已退出连接\n");
        close(netfd);
    }
}
